package com.shizhanzhe.zxbapp.team;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.crazysunj.cardslideview.CardViewPager;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.Util;
import com.shizhanzhe.zxbapp.widget.CircleImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PopPosterActivity extends AppCompatActivity {

    @BindView(R.id.viewpager)
    CardViewPager viewpager;
    private final int[] imageArray = {R.mipmap.pop_invite_bg, R.mipmap.pop_invite_bg1, R.mipmap.pop_invite_bg2};
    @BindView(R.id.save)
    TextView save;
    @BindView(R.id.invite_share)
    FrameLayout inviteShare;
    @BindView(R.id.image)
    ImageView image;
    @BindView(R.id.iv_ewm)
    ImageView ivEwm;
    @BindView(R.id.close)
    ImageView close;
    private int po = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pop_poster);
        ButterKnife.bind(this);
        ivEwm.setImageBitmap(Util.encodeAsBitmap(Path.zxb_register(UserBean.ip,UserBean.uid)));
        final List<MyBean> list = new ArrayList<MyBean>();
        for (int s : imageArray) {
            list.add(new MyBean(s));
        }
        viewpager.bind(getSupportFragmentManager(), new MyCardHandler(), list);
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//                Log.d("MainActivity", "position:" + position + " positionOffset:" + positionOffset + " positionOffsetPixels:" + positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                Log.d("PopPosterActivity", "position:" + position);
                po = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                Log.d("PopPosterActivity", "state:" + state);
            }
        });
        viewpager.setCardTransformer(180, 0.18f);
        viewpager.setCardPadding(15);
        viewpager.setCardMargin(0);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                image.setImageResource(list.get(po).getImg());
                Util.saveBitmap(PopPosterActivity.this, getBitmapByView(inviteShare), System.currentTimeMillis() + ".jpg");
                Toast.makeText(PopPosterActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
            }
        });
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private Bitmap getBitmapByView(View headerView) {
        int h = headerView.getHeight();
        Bitmap bitmap = Bitmap.createBitmap(headerView.getWidth(), h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        headerView.draw(canvas);
        return bitmap;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
